import json
import os
from selenium import webdriver
from zhilian.settings import ROOT_DIR
from test.settings import ZHIHU_COOKIE_FILE
import time
import random

with open(ZHIHU_COOKIE_FILE,'r') as f:
    cookies_json = f.read()

cookies = json.loads(cookies_json)
path =os.path.join(ROOT_DIR,'chromedriver')
browser = webdriver.Chrome(executable_path=path)
browser.get('https://www.zhihu.com/404')
# cookies = make_cookie()
#访问知乎首页

for k in cookies:
    browser.add_cookie(k)
browser.get('http://www.zhihu.com')
js = """document.getElementById("root").scrollIntoView(false)"""

while 1:
    browser.execute_script(js)
    sleep_time = random.randint(1,10)
    time.sleep(sleep_time)
    a_list = browser.find_elements_by_css_selector('a[data-za-detail-view-element_name]')
    for a in a_list:
        href = a.get_attribute('href')
        print(href)
browser.close()